package cn.com.shaom.learning.sb.web.websocket.topic;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

/**
 * ClassName: WebSocketConfig
 * Description: 广播的配置，需要@EnableWebSocketMessageBroker 的基础上，继承 AbstractWebSocketMessageBrokerConfigurer
 * Date: 2017/12/26 9:15 【需求编号】
 *
 * @author Shaom
 * @version V1.0.0
 */
@Configuration
// 开启使用stomp 协议来传输基于代理（message broker）的消息
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {


    /**
     * 注册Stomp 的endpoint,并且映射URL
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //注册一个，并且制定使用 SockJS
        registry.addEndpoint("/endpointPubSubWs").withSockJS();
    }

    /**
     * 配置消息代理
     *
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // topic-配置的广播消息代理
        registry.enableSimpleBroker("/topic");
    }
}
